<template>
<van-button class="follow-btn" round size="small" v-if="is_followed" @click="onFollow">已关注</van-button>
<van-button v-else class="follow-btn" type="info" color="#3296fa" round size="small" icon="plus" @click="onFollow">关注</van-button>
</template>

<script>
import { addFollow, deleteFollow } from '@/api/user'
export default {
  components: {},
  model: {
    prop: 'is_followed',
    event: 'update_is_followed'
  },
  props: {
    is_followed: {
      type: Boolean,
      required: true
    },
    user_id: {
      type: [Number, String, Object],
      required: true
    }
  },
  data () {
    return {
      isFollowLoading: false
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onFollow () {
      this.isFollowLoading = true
      try {
        // 如果已关注，则取消关注
        if (this.is_followed) {
          await deleteFollow(this.user_id)
        } else {
          // 否则添加关注
          await addFollow(this.user_id)
        }
        this.$emit('update_is_followed', !this.is_followed)
        // 更新视图
      } catch (err) {
        console.log(err)
        this.$toast.fail('操作失败')
      }
      this.isFollowLoading = false
    }
  }
}
</script>

<style scoped>

</style>
